import { http } from '../api'

const WxPushMessage = {
    tmplIds: [],
    showPop:false, //是否有弹框了
    //获取一次所有的消息id     1 普通用户  2商户  3配送人员
    initConfig:function(){

        let dicts = uni.getStorageSync('miniProgramTemplateIdEnumList')

        if(dicts){

            this.tmplIds = dicts

            return
        }

        http({'url':"common/get/enums"})
        .then( res =>{

            let data = res?.data?.miniProgramTemplateIdEnumList

            if(data){

                this.tmplIds = data

                uni.setStorageSync('miniProgramTemplateIdEnumList',data)

            }
           
        })
    },
    //type     1 普通用户  2商户  3配送人员
    needTmpIds:function( type  ){

        let arr = []

        this.tmplIds.filter( item =>{

            if((item.value+'').includes(type)){

                arr.push( item.code )
            }

        })

        return arr
    },
    //获取是否已经受过权了  needTip:是否要提醒  
    // return中 1：订阅消息未开启  2 消息打开了，没有订阅   3 订阅了一部分，没订阅全   4订阅了全部  5 拒绝了订阅
    initType: function ( type , cb ,needTip  ) {

        const hasKeyArr = this.needTmpIds( type )

        // console.log('几个',type ,hasKeyArr )

        let that = this

        uni.getSetting({
            withSubscriptions: true,
            success(res) {
                console.log('授权信息', res)
                if (res.subscriptionsSetting.mainSwitch) {

                    if (res.subscriptionsSetting.itemSettings != null) {


                        let msgType = 4

                        hasKeyArr.map( itemKey =>{

                            let item = res.subscriptionsSetting.itemSettings[itemKey]
                            if (item == 'reject') {
                                console.log('提示：您已经拒绝订阅消息')

                                msgType = 3

                                if(needTip){
                                    that.openSet()
                                }
                                
                            } else if (item == 'accept') {
                                console.log('提示：您已经开启订阅消息')

                               
                            } else if (item == 'ban') {

                                console.log('提示：您已经被后台封禁')

                                msgType = 3
                            }

                        })

                        //如果权是已开了的 就循环多次 累计多个消息次数
                        if( msgType == 4 ){

                            console.log('走了累加',4)

                            let timer = 0

                            while( timer < 100){

                               timer++

                                that.authorizationBtn( type )
                            }

                        }

                        cb && cb( msgType )

                    
                        return
                    }
                    
                    cb && cb( 2 )

                } else {
                    // console.log('提示：订阅消息未开启')

                    if(needTip){
                        that.openSet()
                    }
                    else{
                        cb && cb( 1 )
                    }

                    
                }
            },
        })
    },
    //打开设置
    openSet:function(){

        if(this.showPop) return 

        this.showPop = true 

        setTimeout(()=>{

            this.showPop = false

        },1000)

        uni.showModal({
            title: '未开启订阅消息',
            content: '请开启订阅消息，方便接收通知',
            confirmText: "打开设置",
            cancelText: "取消",
            success: function(res) {
                if (res.confirm) {

                    // console.log('9999999999申请消息',res)

                    uni.openSetting({
                        success(res) {
                            console.log(res.authSetting)
                        }
                    });

                }
            }
        })

    },
    // 授权
    // 返回 1代表失败 2 用户拒绝了授权  3授权通过
    authorizationBtn:function(type, cb) {

        console.log('执行了n多次11111111111111111111')

        let taht = this

        const hasKeyArr = this.needTmpIds( type )

        if(this.showPop) return 

        wx.requestSubscribeMessage({
            tmplIds: hasKeyArr,
            success(res) {

                console.log('点击授权成功',res)
                let bool = 3
                
                // //判断用户是否点同意所有 并且点了总是保持 

                // if( taht.nums != 2){

                //     taht.nums == 2

                //     taht.authorizationBtn( type, cb )

                //     return
                // }

             
                

                hasKeyArr.map( key =>{

                    if(res[key] == 'reject'){

                        bool = 2
                    }

                })

                //true  代表有拒绝的
                cb &&  cb( bool )
            },
            fail: function(error){

                console.log('点击授权失败了',error);

                // uni.showToast({title:'授权失败',icon:'none'})

                cb &&  cb( 1 )
            }
        })
    },
   
}

WxPushMessage.initConfig()

export default WxPushMessage
